/*
 * libroxsd - a portable, lightweight XSD validation library
 * Copyright (C) 2012  Emmanuel Deloget
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor,
 * Boston, MA  02110-1301  USA
 */

#ifndef roxsd_compiler_xsdtype_h_included
#define roxsd_compiler_xsdtype_h_included

#include <stdlib.h>
#include "roxsd-list.h"

typedef struct xsdtype_s xsdtype_t;

struct xsdtype_s {
	list_define_header();
	char *name;
	char *tname;
	int type;
	int minocc;
	int maxocc;
	xsdtype_t *children;
};

typedef enum {
	ROXSDI_TYPE_CHOICE,
	ROXSDI_TYPE_SEQUENCE,
	ROXSDI_TYPE_SIMPLETYPE,
	ROXSDI_TYPE_COMPLEXTYPE,
	ROXSDI_TYPE_ELEMENT,
	ROXSDI_TYPE_ATTRIBUTE,
	ROXSDI_TYPE_SKIP,
	ROXSDI_TYPE_MASK = 0x7fff,
	ROXSDI_TYPE_CHECKED = 0x8000,
	ROXSDI_TYPE_LAST
} xsdttype_t;

#define UNBOUNDED (-1)

xsdtype_t *roxsdi_xsdtype_new(
				char *ename,
				char *etype,
				int minocc,
				int maxocc,
				int ttype);

xsdtype_t *roxsdi_xsdtype_add_element(
				xsdtype_t *type,
				char *ename,
				char *etype);

void roxsdi_xsdtype_free(
				xsdtype_t *t);

/** @brief Find a named type in the list
 *
 * @param head type list head
 * @param etype type name
 * @return the found type object or NULL
 */
xsdtype_t *roxsdi_xsdtype_find(
				xsdtype_t *head,
				char *etype);

/* helper functions */
static inline xsdtype_t *roxsdi_choice_new(int minocc, int maxocc)
{
	return roxsdi_xsdtype_new(NULL, NULL, minocc, maxocc, ROXSDI_TYPE_CHOICE);
}

static inline xsdtype_t *roxsdi_sequence_new(void)
{
	return roxsdi_xsdtype_new(NULL, NULL, 1, 1, ROXSDI_TYPE_SEQUENCE);
}

static inline xsdtype_t *roxsdi_element_new(char *ename, char *etype)
{
	xsdtype_t *type = roxsdi_xsdtype_new(ename, etype, 1, 1, ROXSDI_TYPE_ELEMENT);
	return type;
}

static inline xsdtype_t *roxsdi_xsdtype_add_child(xsdtype_t *parent, xsdtype_t *child)
{
	parent->children = list_pushback(parent->children, child);
	return child;
}

#endif /* roxsd_compiler_xsdtype_h_included */
